﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SiteAgle.Models;

namespace SiteAgle.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class SprintController : GeneralController
    {
        DaoSprint dao;
        /// <summary>
        /// Initializes a new instance of the <see cref="SprintController"/> class.
        /// </summary>
        public SprintController()
        {
            dao = new DaoSprint();
        }
        //
        // GET: /Sprint/

        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View(dao.listar());
        }

        //
        // GET: /Sprint/Details/5

        /// <summary>
        /// Detailses the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            return View(dao.ObterPorID(id));
        }

        //
        // GET: /Sprint/Create

        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns></returns>
        public ActionResult Novo()
        {
            return View(new Sprint());
        } 

        //
        // POST: /Sprint/Create

        /// <summary>
        /// Creates the specified sprint.
        /// </summary>
        /// <param name="sprint">The sprint.</param>
        /// <param name="collection">The collection.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Novo(Sprint sprint,FormCollection collection)
        {
           
                sprint.Criacao = DateTime.Now;
                // TODO: Add insert logic here
                sprint.Projeto = new DaoProjeto().ObterPorID(sprint.IdProjeto);
            

                dao.Salvar(sprint);
                Session["sucesso"] = "Criado com sucesso";
                return RedirectToAction("Index");
           
        }
       
        //
        // GET: /Sprint/Edit/5

        /// <summary>
        /// Edits the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public ActionResult Editar(int id)
        {

            return View(dao.ObterPorID(id));
        }

        //
        // POST: /Sprint/Edit/5

        /// <summary>
        /// Edits the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="collection">The collection.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Editar(Sprint sprint,  FormCollection collection)
        {
            try
            {
                Sprint antSprint = dao.ObterPorID(sprint.Id);
                antSprint.Descricao = sprint.Descricao;
                antSprint.Inicio = sprint.Inicio;
                antSprint.Fim = sprint.Fim;                
                // TODO: Add update logic here
                dao.Salvar(antSprint);
                Session["sucesso"] = "Salvo com sucesso";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Sprint/Delete/5

      

        //
        // POST: /Sprint/Delete/5

        /// <summary>
        /// Deletes the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="collection">The collection.</param>
        /// <returns></returns>
   
        public ActionResult Excluir(int id, FormCollection collection)
        {
            try
            {
                DaoSprint dao = new DaoSprint();
                Sprint sprint = dao.ObterPorID(id);
                if (sprint.Tarefas.Count() == 0 )
                {
                    dao.Excluir(sprint);
                    Session["Sucess"] = "Sprint Excluida com sucesso";
                }
                else
                {
                    Session["Error"] = "Exclua primeiro as Tarefas dessa Sprint";
                }
                return RedirectToAction("Index");
                // TODO: Add delete logic here
              
            }
            catch
            {
                return View();
            }
        }
    }
}
